import CryptoJS from 'crypto-js'

const key = CryptoJS.enc.Utf8.parse('Taipingtpzx20231')
const iv = CryptoJS.enc.Utf8.parse('Taipingtpzx20231')
/**
 *
 * @method encrypts
 * @description 加密数据
 * @example encrypts(data)
 * @params { unknown } data
 * @params { string } key = '1234567890000000'
 * @params { string } iv = '1234567890000000'
 * @returns boolean
 * @version v1.0.0
 * @author liuxu 2022-12-07
 *
 */

export function encrypts(data: string | Record<string, unknown>): string {
  const string = JSON.stringify(data)
  return CryptoJS.AES.encrypt(string, key, {
    iv,
    padding: CryptoJS.pad.Pkcs7,
    mode: CryptoJS.mode.ECB,
  }).toString()
}

/**
 *
 * @method decrypts
 * @description 解密数据
 * @example decrypts(data)
 * @params { unknown } data
 * @params { string } key = '1234567890000000'
 * @params { string } iv = '1234567890000000'
 * @returns boolean
 * @version v1.0.0
 * @author liuxu 2022-12-07
 *
 */

export function decrypts(data: string): string {
  return JSON.parse(
    CryptoJS.AES.decrypt(data, key, {
      iv,
      padding: CryptoJS.pad.Pkcs7,
      mode: CryptoJS.mode.ECB,
    }).toString(CryptoJS.enc.Utf8),
  )
}
